//
//  XmlView.m
//  云成绩
//
//  Created by Apple1 on 2017/3/2.
//  Copyright © 2017年 zj. All rights reserved.
//

#import "XmlView.h"

@implementation XmlView {
    NSArray<NSArray<UILabel *> *> *textLabels;
    
    UIView *headBackgroundView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)setUpLabels {
    for (NSArray *array in textLabels) {
        [array makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    CGFloat height = CGRectGetHeight(self.bounds);

    if (headBackgroundView == nil) {
        headBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _headWidth, height)];
        headBackgroundView.backgroundColor = [UIColor whiteColor];
        [self insertSubview:headBackgroundView atIndex:0];
    }
    
    CGFloat _lineHeight = _datas.count > 0 ? height / _datas.count : height;
    
    CGFloat onePx = 1 / [UIScreen mainScreen].scale;
    
    NSMutableArray *labels = [NSMutableArray arrayWithCapacity:_datas.count];
    for (int i = 0; i<_datas.count; i++) {
        NSArray *array = _datas[i][k_HeaderDataArray];
        NSMutableArray *subLables = [NSMutableArray arrayWithCapacity:array.count + 1];

        [labels addObject:subLables];
        
        UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, i * _lineHeight, _headWidth - onePx, _lineHeight - onePx)];
        headLabel.textAlignment = NSTextAlignmentCenter;
        headLabel.textColor = _headColor ?: [UIColor whiteColor];
        headLabel.backgroundColor = self.tintColor;
        headLabel.attributedText = _datas[i][k_HeaderAttrString];
        headLabel.numberOfLines = _numberOfLines;
        headLabel.font = [UIFont systemFontOfSize:_fontSize];
        headLabel.adjustsFontSizeToFitWidth = YES;

        [subLables addObject:headLabel];
        
        [self addSubview:headLabel];
        
        if (array.count == 0) continue;
        
        CGFloat width = (CGRectGetWidth(self.bounds) - _headWidth) / array.count;
        for (int j = 0; j < array.count; j++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_headWidth + j * width, i * _lineHeight, width - (j == array.count - 1 ? 0 : onePx), _lineHeight - (i == _datas.count - 1 ? 0 : onePx))];
            
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = _fontColor ?: [UIColor whiteColor];
            label.attributedText = array[j];
            label.numberOfLines = _numberOfLines;
            label.backgroundColor = [UIColor whiteColor];
            label.adjustsFontSizeToFitWidth = YES;
            label.font = [UIFont systemFontOfSize:_fontSize];
            [subLables addObject:label];
            
            [self addSubview:label];
        }
    }
    textLabels = labels;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat height = CGRectGetHeight(self.bounds);
    
    headBackgroundView.frame = CGRectMake(0, 0, _headWidth, height);
    
    CGFloat _lineHeight = textLabels.count > 0 ? height / textLabels.count : height;
    
    CGFloat onePx = 1 / [UIScreen mainScreen].scale;
    
    for (int i = 0; i < textLabels.count; i++) {
        NSArray<UILabel *> *labels = textLabels[i];
        labels[0].frame = CGRectMake(0, i * _lineHeight, _headWidth - onePx, _lineHeight - onePx);
        
        if (labels.count == 1) continue;
        
        CGFloat width = (CGRectGetWidth(self.frame) - _headWidth) / (labels.count - 1);
        
        for (int j = 1; j < labels.count; j++) {
            labels[j].frame = CGRectMake(_headWidth + (j - 1) * width, i * _lineHeight, width - (j == labels.count - 1 ? 0 : onePx), _lineHeight - (i == textLabels.count - 1 ? 0 : onePx));
        }
    }
}

-(void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    if (newSuperview == nil) {
        [headBackgroundView removeFromSuperview];
        for (NSArray *array in textLabels) {
            
            [array makeObjectsPerformSelector:@selector(removeFromSuperview)];

        }
        textLabels = nil;
        headBackgroundView = nil;
        return;
    }
    
    [self setUpLabels];
}

- (void)setDatas:(NSArray<__kindof NSDictionary *> *)datas {
    _datas = datas;
    [self setUpLabels];
}

@end
